<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<div class="pro_list fr"  >
   <div class="title"  >
   	 <span class="titletxt">您现在的位置：</span>
     <ul> 
       <li class="root">会员中心</li>        
       <li>订单管理</li>  
       <li>我的订单</li>            
     </ul>
   </div>
  <c:if test="${not empty ret }">
   	<c:if test="${ret.code ne '200'}">
   		<script type="text/javascript">alertError('${ret.result}');</script>
   		<div id=messager class="messager" >
	         <div class="contentmsg">
	             <h5>提示信息～！</h5>
	             <ul>
	               <li>${ret.result}</li>
	             </ul>
	        </div>
	     <div class="closer" onClick="showHideLayer('messager');" title="关闭"></div>
	    </div>
   	</c:if>
   </c:if>
<br/>
<div  class="easyui-panel" title="我的订单"  style="">

<div data-options="region:'north',title:'',split:true" style="height:40px;background-color:#e4e4e4;">
    	<form action="" id="searchForm" name="searchForm">
    	<table>
    		<tr>
				<td class="screen-n"><span>订单编号</span></td>
				<td class="screen-n">
					<input type="text" id="orderCode" class="screen-input" onkeydown="pms.kd13('initProducts')">
				</td>
				<td class="screen-n"><span>订单状态</span></td>
				<td class="screen-n">
					<select id="status" class="easyui-combobox">
						<option value="">-全部-</option>
						<option value="10">-已确认-</option>
						<option value="14">-已审核-</option>
						<option value="20">-已支付-</option>
						<option value="50">-已出库-</option>
						<option value="60">-已收货-</option>
						<option value="-1">-已取消-</option>
					</select>
				</td>
				<td class="screen-n"><span>订单时间</span></td>
				<td class="screen-n">
					<input type="text" id="startDate" class="screen-input easyui-datebox" onkeydown="pms.kd13('initProducts')">
				</td>
				<td class="screen-n"><span>至</span></td>
				<td class="screen-n">
					<input type="text" id="endDate" class="screen-input easyui-datebox" onkeydown="pms.kd13('initProducts')">
				</td>
				<td class="screen-n">
					<input type="button" class="button" value="查询" onclick="initProducts()">
					<input type="button" class="button" value="重置" onclick="pms.resetForm('searchForm')" >
				</td>
			</tr>
			</table>
		</form>
    </div>
    
    <div data-options="region:'center',split:true" style="width: 100%;height:431px">
		<table id="tableGrid" ></table>
	</div>
	<form  action='<c:url value="/payment/payOrder.s"/>' method="post" name="payOrderForm">
	　　　<input type="hidden" name="code" id="code"/>
　　</form>
</div>
</div>

<div id="dlg-buttons" style="display: none;">
     <table cellpadding="0" cellspacing="0" style="width: 90%">
         <tr>
             <td style="text-align: center;">
                 <a href="#" class="easyui-linkbutton"   iconcls="icon-save"  onclick="javascript:savecompact()">同意</a>
                 <a href="#" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#pay_compact_win').dialog('close')">拒绝</a>
             </td>
         </tr>
     </table>
</div>
<script>
var productsTable;
$(function(){
	initProducts();
});

function initProducts(){
	productsTable = $('#tableGrid').pDataGrid({
		 columns:[[
	          	  {field:'orderCode',title:'订单编码',align:'center',sortable:true,formatter:function(value,row){
						return '<a href="javascript:openOrderDetailWindow(\''+row.orderCode+'\',\''+row.isInvoice+'\')">'+value+'</a>';
		          }},
	          	  {field:'buyerName',title:'采购单位',align:'center',sortable:true},
	          	  
	          	  {field:'totalMoney',title:'下单金额(元)',align:'center',sortable:true},
	          	  {field:'confirmMoney',title:'确认金额(元)',align:'center',sortable:true},
	              /* {field:'tradeFeeMoney',title:'交易服务费(元)',align:'center',sortable:true}, */
	          	  {field:'createDateStr',title:'下单时间',align:'center',sortable:true},
	          	  {field:'userName',title:'下单人',align:'center',sortable:true},
	          	  {field:'status',title:'订单状态',align:'center',sortable:true,formatter:function(value,row){
					 switch(row.status){
					case -1:
						  return '已取消';
						  break;
					case 10:
						  return '已确认';
						  break;
					case 14:
						  return '已审核';
						  break;
					case 20:
						  return '已支付';
						  break;
					case 50:
						  return '已发货';
						  break;
					case 60:
						  return '已收货';
						  break;
					}
		          }},
	              {field:'action',title:'操作',align:'center',sortable:true,formatter:function(value,row){
		              var str = '';
		              if(row.status >=10 && 20>row.status){
		            	 //str+='<img class="iconImg ext-icon-computer_go" title="在线支付" onclick="payOrder(\''+row.orderCode+'\','+row.confirmMoney+');"/>&nbsp;';
						 str+='<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="payOrder(\''+row.orderCode+'\','+row.confirmMoney+')">在线支付</a>&nbsp;';
			          }
			          if(row.status >=10 && 14>=row.status){
			        	 //str+='<img class="iconImg ext-icon-hourglass_delete" title="取消订单" onclick="canlOrder(\''+row.orderCode+'\');"/>&nbsp;';
			        	 str+='<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="canlOrder(\''+row.orderCode+'\')">取消订单</a>&nbsp;';
			          }
			          if(row.status>=10){
			        	 //str+='<img class="iconImg ext-icon-basket_put" title="资料打包" onclick="fetchOrderPaper(\''+row.orderCode+'\');"/>&nbsp;';
					 	str+='<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="fetchOrderPaper(\''+row.orderCode+'\')">资料打包</a>&nbsp;';
						<%-- str+='<a href="<%=request.getContextPath()%>/order/uploadOrderQuality.do?orderCode=${row.orderCode}">资料打包</a>'; --%>
				      }
			          if(row.status==50){
			        	  //str+='<img class="iconImg ext-icon-accept" title="确认收货" onclick="confirmOrder(\''+row.orderCode+'\');"/>&nbsp;';
			        	  str+='<a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="confirmOrder(\''+row.orderCode+'\')">确认收货</a>&nbsp;';
				      }
					  return str;
		           }}
		          ]],
		    url:'<c:url value="/trade/searchPageOrder.s"/>',  
		    pagination:true,
		    pageSize:30,
		    queryParams:{
		    	 "orderCode":$("#orderCode").val(),
				 "status":$('#status').combobox('getValue'),
				 "startDate":$("#startDate").datebox("getValue"),
				 "endDate":$("#endDate").datebox("getValue")
		    },
		    onDblClickRow:function(index,row){
		    	openOrderDetailWindow(row.orderCode,row.isInvoice);
			},
			completeCallback:function(data){
				if(data.code == "100"){
					alertError(data.result);
				}else{
					 
				}
			}
	}); 
}
function openOrderDetailWindow(code,isInvoice){
	var param = {"orderCode":code,"isInvoice":isInvoice};
	$.pmsDialog({
		id:"win_order_detail",
		title:'订单号：'+code+" 明细",
		href:'<c:url value="/views/jsp/center/trade/order/order_detail.jsp"/>?printable=true',
		width:850,
		height:500,
		params:param,
		buttons:[]
	});
}
function payOrder(code,money){

	//检查支付协议是否签署
	$.pmsAjax({
		url:'<c:url value="/user/checkPayCompact.s"/>',
		data:{},
		success:function(data){
			if(data.code=="200"){
				var param = {"code":code,"money":money};
				$.pmsDialog({
					id:'win_order_pay',
					title:'订单支付方式选择',
					href:'<c:url value="/views/jsp/center/trade/pay/pay_list.jsp"/>',
					width:1000,
					height:600,
					params:param
				});
			}else{
				//弹出 签署协议
				//弹出合同签订
				$.pmsDialog({
					id:'pay_compact_win',
					title:'在线支付协议',
					method:'post',
					href:'<c:url value="/views/jsp/center/compact/pay_compact_detail.jsp"/>',
					width:800,
					height:500,
					buttons: '#dlg-buttons'
				});
			}
		}
	});
	
	//$("#orderCode").val(orderCode);
	//document.forms["payOrderForm"].submit();
	
}
function canlOrder(orderCode){
	if(confirm('确认取消订单？')){
		var param = {"orderCode":orderCode};
		$.pmsAjax({
			url:'<c:url value="/trade/cancelOrder.s"/>',
			data:param,
			success:function(data){
				if(data.code=="200"){
					layer.msg("取消订单成功",1, {type: 1,shade: [0.2, '#ccc']});
					productsTable.refresh();
				}else{
					layer.alert(data.result, 8); 
				}
			}
		});
	} 
}
//确认收货
function confirmOrder(orderCode){
	if(confirm('确认收到货物？确认之后，将打款至卖家账户！')){
		var param = {"orderCode":orderCode};
		$.pmsAjax({
			url:'<c:url value="/trade/confirmOrder.s"/>',
			data:param,
			success:function(data){
				if(data.code=="200"){
					layer.msg("确认收货成功",1, {type: 1,shade: [0.2, '#ccc']});
					productsTable.refresh();
				}else{
					layer.alert(data.result, 8); 
				}
			}
		});
	} 
}
//资料打包下载

function fetchOrderPaper(orderCode){
	var queryParamss = {"orderCode":orderCode};
	loadExcelCallBack('<c:url value="/trade/uploadOrderQuality.s"/>',queryParamss);
} 	
function loadExcelCallBack(dourl,queryParams){
	$.ajax({
	    url : dourl+'?loadType='+$("#loadLimit").val(),
	    pagination:false,
	    type : "POST",
	    dataType : "json",
	    data:queryParams,
	    success : function(data) {
		    console.log(data.data);
		    if(data.code=="100"){
		    	layer.msg("装载要下载的数据出错啦,错误代码："+data.result);
			}else if(data.code=="101"){
		    	layer.msg("装载要下载的数据出错啦,错误代码："+data.result);
			}else{
				//parent.window.open('<c:url value="/util/load.do?"/>fileName='+data.data.outPath+'&title='+data.data.title);
				location.href = '<c:url value="/util/load.s?"/>fileName='+data.data.outPath+'&title='+data.data.title;
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown) {
		    layer.msg(" --下载出错啦 请联系管理员!");
		}
	});
}
function savecompact(){
	$.pmsAjax({
		url:'<c:url value="/user/signPayCompact.s"/>',
		data:{},
		success:function(data){
			if(data.code=="200"){
				layer.msg("签订协议成功",1, {type: 1,shade: [0.2, '#ccc']});
				$("#pay_compact_win").dialog('close');
			}else{
				alertError(data.result);
			}
		}
	});
}
</script>